package com.mikasa.springboot.example.controller;

import com.alibaba.fastjson.JSON;
import com.mikasa.springboot.example.domain.RedisMessage;
import com.mikasa.springboot.example.domain.User;
import com.mikasa.springboot.example.redis.JedisSubscribeTasker;
import com.mikasa.springboot.example.redis.JedisTemplate;
import com.mikasa.springboot.example.redis.RedisUtil;
import com.mikasa.springboot.example.redismq.UserMessageListener;
import com.mikasa.springboot.example.redismq.UserMessageProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
@RequestMapping("/redis")
public class RedisController {
	
	private static final Logger log = LoggerFactory.getLogger(RedisController.class);

    private static final ExecutorService threadPool = Executors.newFixedThreadPool(10);

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private JedisTemplate jedisTemplate;

    @Autowired
    private JedisSubscribeTasker jedisSubscribeTasker;

    @Autowired
    private UserMessageListener userMessageListener;

    @Autowired
    private UserMessageProvider userMessageProvider;

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test() {
        log.info("redis测试开始...");
        redisUtil.set("test1","test1");
        redisUtil.sendMessage("default", "default-message");
        User user = new User();
        user.setId(1);
        user.setPassword("123654");
        user.setPhone("13011111111");
        user.setUsername("sherlock");
        redisUtil.sendMessage("jedis.user",JSON.toJSONString(user));
        return "success";
    }

    @RequestMapping(value = "/test/jedis", method = RequestMethod.GET)
    public String testJedis() {
        log.info("jedis测试开始...");
        String message = "default-message";
        jedisTemplate.publish("default",message);
        //订阅消息
        String channel = "jedis.user";
        /*jedisSubscribeTasker.setJedisPubSub(userMessageListener);
        jedisSubscribeTasker.setChannel(channel);
        threadPool.execute(jedisSubscribeTasker);*/
        //发布消息
        message = "user-message";
        ConcurrentHashMap<String,Object> map = new ConcurrentHashMap<String,Object>();
        map.put("channel",channel);
        map.put("message",message);
        userMessageProvider.setMap(map);
        threadPool.execute(userMessageProvider);
        return "success";
    }

    @RequestMapping(value = "/push", method = RequestMethod.GET)
    public String push() {
        log.info("redis队列测试开始...");
        User user = new User();
        user.setId(1);
        user.setPassword("123654");
        user.setPhone("13011111111");
        user.setUsername("sherlock1");
        User user1 = new User();
        user1.setId(2);
        user1.setPassword("1236541");
        user1.setPhone("13011111112");
        user1.setUsername("sherlock2");
        User user2 = new User();
        user2.setId(3);
        user2.setPassword("1236542");
        user2.setPhone("13011111113");
        user2.setUsername("sherlock3");
        redisUtil.lpush("user_list",user);
        redisUtil.lpush("user_list",user1);
        redisUtil.lpush("user_list",user2);
        log.info("队列中的数据:"+redisUtil.range("user_list"));
        String channel = "jedis.user";
        String message = "user-push";
        RedisMessage redisMessage = new RedisMessage();
        redisMessage.setCode(channel);
        redisMessage.setMessage(message);
        //redisUtil.sendMessage(channel,JSON.toJSONString(redisMessage));
        jedisTemplate.publish(channel,JSON.toJSONString(redisMessage));
        return "success";
    }
	
}
